Programming for Data Science¶

Variables, Operators¶

Dr. Bhargavi R

SCOPE, VIT Chennai

  • Python is easy to learn
  • Guido Van Rossum – Creator - early 1990s
  • Interpreted
  • Object oriented
  • Dynamic typed language
    • Declaration of the variable is not required
    • Data type of the variable can be changed dynamically
    • For Example:
      • price = 100
      • total = 100 * 10
      • price = 56.50

Let's start Python with printing ___¶

In [1]:
print("Welcome to Programming for Data Science")
Welcome to Programming for Data Science

Comments in Python¶

In [2]:
# Pound symbol indicates the beginning of a comment

""" This is a 
multiline comment"""
''' This is a 
multiline comment'''
Out[2]:
' This is a \nmultiline comment'
In [3]:
print(100)
print("Here we go!")
print('Here we go!')
print("let's start")
100
Here we go!
Here we go!
let's start
In [4]:
print('let's start') 
  Input In [4]
    print('let's start')
                      ^
SyntaxError: unterminated string literal (detected at line 1)
In [ ]:
print('let\'s start')

Assigning values to variables¶

In [ ]:
# Define a variable apple_price and assign a value 12 to it
apple_price = 12
print(apple_price)
In [ ]:
# Define a floating point variable height and assign a value 5.2 
height = 5.2
print(height)
In [ ]:
# Define three varibles mark1, mark2, mark3 and assign the values 67,47.5, 86 respectively
mark1, mark2, mark3 = 67, 47.5, 86
print(mark1,'\n', mark2, '\n', mark3)

mark1, mark2 = mark2, mark1
print('mark1 =', mark1, 'mark2 = ', mark2)
# This is like the following code in other prgramming languages
# temp = mark1
# mark1 = mark2
# mark2 = temp
In [ ]:
print(mark1, mark2, mark3)
print(mark1, mark2, mark3, sep= ' ')
print(mark1, mark2, mark3, sep='\t')
print(mark1, mark2, mark3, sep='\n')

Complex Numbers (Optional)¶

  • A complex number consists of an ordered pair of real floating point numbers denoted by a + bj
  • Here a is the real part and b is the imaginary part of the complex number.
  • complex(x) -  Converts x to a complex number with real part x and imaginary part zero.
  • complex(x, y) - Convertc x to a complex number with real part x and imaginary part y
In [ ]:
c1 = complex(2)
print("c1 = ",c1)
c2 = complex(2, 3)
print("c2 = ", c2)
In [ ]:
a = 3 + 4j     # Define a complex variable a which is asigned a value of 3 + 4j
b = 2 + 5j     # Define a complex variable b which is asigned a value of 2 + 5j
print(a)
In [ ]:
my_sum = a + b
print(my_sum)
In [ ]:
print( 3 + 4j * 2) 
print((3 + 4j) * 2)
In [ ]:
# Print the real part of a complex nember
print(my_sum.real)

# Print the imaginary part of a complex nember
print(my_sum.imag)

Working with boolean¶

In [ ]:
is_raining = True
print(is_raining)
In [ ]:
is_raining = 25
print(bool(is_raining))
In [ ]:
condition = 1 < 10
print(condition)
print ( bool(condition))
print (bool(False))
print (bool(True))
In [ ]:
x = 5
y = 0
print (bool(x))
print (bool(y))
print(bool(-12))
In [ ]:
char = 'a'
nullChar = ''
print (bool(char))
print (bool(nullChar))

type¶

In [ ]:
print(type(True))
print(type(mark1))
print(type(mark2))
  • Python is dynamic typed.
In [ ]:
mark1 = 60
print(type(mark1))
mark1 = 56.5
print(type(mark1))

Type Conversion¶

In [ ]:
x = 10  # x is defined as a integer variable
print(type(x))

y = float(x) # convert x to floating variable and assign it to y
print(type(y))
  • In python a string is a sequence of characters. A string is surrounded by a matchind pair of '' or ""
In [ ]:
institute_name = "VIT Chennai"
place = 'Chennai'
print(institute_name, end = ',')
print(place)
  • Binary, Octal and Hexadecimal numbers
In [ ]:
bin1 = 0b011 # bin1 is a binary variable asigned a value of 011
print(bin1)
print(bin(bin1))
In [ ]:
oct1 = 0o10 # oct1 is an octal variable asigned a value of 8
print(oct1)
print(oct(oct1))
In [ ]:
hex1 = 0x16 # hex1 is a hexadecimal number with a value of 16
print(hex1)
print(hex(hex1))

User input in python¶

  • input() function is used to get the user input at run time
  • By default, input function reads the input as a string.
  • So we should change the data to appropriate data type immediately after reading from the user
In [ ]:
x = input('Enter the value of x: ')
print(type(x))
In [ ]:
x = int(x)
print(type(x))
In [ ]:
# Alternatively we can also code as follows

y = int(input('Enter the value of y: '))
print(type(y))

Assignment Operator¶

In [ ]:
# A simple asignmnet
number = 10
print(number)
In [ ]:
String = "Name"
print(String)
In [ ]:
# Tuple assignment positional
num1, num2 = 56, '45'
print(num1, num2, sep = ',')
In [ ]:
# Multiple target assignment
num1 = num2 = 100
print(num1, num2, sep = ',')
In [ ]:
# Augmented assignment
num1 += num2
print("num1 = ", num1)
In [ ]:
num = 10
id(num)

Arithmetic Operaters¶

image.png

Logical operators¶

image.png

Comparison¶

Also known as Relational Operators. Operate on two operands Evaluates to a boolean value

image.png

In [ ]:
my_num = 222
result = (my_num % 2) == 0
print(my_num, 'is even -', result)

my_num = 101
print(my_num, 'is even -', my_num % 2 == 0)

Bitwise operators¶

These opertors perform bit-by-bit operation on tthe opernds image.png

In [ ]:
x = 0b1011 # 11
y = 0b1101 # 13
print(bin(x & y))
print(bin(x | y))
print(bin(x ^ y))
In [ ]:
x = 0b001000
print(x >> 1)
print(bin(x >> 1))
In [ ]:
print(bin(x))
print( bin( x << 2))
print(bin(x))

Membership Operators¶

image.png

Identity Operators¶

image.png

Operator precedence¶

Operators in the same box group left to right (except for exponentiation, which groups from right to left). image.png